package com.food.user.consumer;

import com.food.user.entity.Order;
import com.food.user.entity.OrderLog;
import com.food.user.service.OrderLogService;
import com.food.user.service.OrderService;
import com.food.user.util.DateUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
@Component
@RocketMQMessageListener(consumerGroup = "checkOrder",topic = "order_check")
public class CheckOrder  implements RocketMQListener<String>{
        @Autowired
        private OrderService orderService;
        @Autowired
        private OrderLogService orderLogService;
        @Override
        @Transactional
        public void onMessage(String s) {
            Order byId = orderService.getById(s);
            if (byId.getOrderState()==5){
                byId.setOrderState(6);
                byId.setOrderStateName("订单已取消");
                byId.setRejectReason("规定时间内未支付");
                byId.setOrderCancelDate(DateUtils.parseString(LocalDateTime.now(),"yyyy-MM-dd HH:mm:ss"));
                orderService.updateById(byId);
            }
            //记录日志
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderLogOrderNum(byId.getOrderNum());
            orderLog.setOrderLogOrderStateId(6);
            orderLog.setOrderLogOrderState("订单已取消");
            orderLog.setOrderLogOperName("系统自动取消");
            orderLog.setOrderLogOperUser("系统自动");
            orderLog.setOrderLogOperDate(DateUtils.parseString(LocalDateTime.now(),"yyyy-MM-dd HH:mm:ss"));
            orderLog.setOrderLogOperContent("系统自动取消");
            orderLogService.save(orderLog);
        }

    }

